/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-26 14:05:23
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-13 16:45:58
 */

import { goodsAPI } from "../../../api/index";

import {
  returnStatus,
  returnStatusToast
} from './../../../utils/index'
// pages/mine/collection/index.js
var appInst =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    requestObj: {},
    records:[],
    goods:[
      {
        src:'/pages/images/1.jpg',
        name:'宽松短袖领带双曲纱T恤',
        price:'$16'
      },
      {
        src:'/pages/images/18.jpg',
        name:'宽松短袖领带双曲纱T恤',
        price:'$16'
      }
    ]
  },
  // 导航去商品详情
  toDetail({detail,target:{dataset:{productid:productId}}}) {
    if(detail==='cell'){
      wx.navigateTo({
        url: `/pages/goods/detail/index?id=${productId}`
      });
    }
  },
  onClose({detail:{position,instance}}){
    if(position==='right'){
      instance.close();
    }
  },
  // 删除收藏夹
  async dropCollect({target:{dataset:{productid:productId}}}){
    const res = await goodsAPI.handleCollect({productId,type:"NO"})
    const _ = await returnStatus(res)
    if(_){
      wx.showToast({
        title: '移除成功',
        icon: 'success',
        image: '',
        duration: 800,
      });
      setTimeout(() => {
        this.setData({
          records:[],
          pageNum:1
        })
          this.getData();
      }, 800);
    }
  },
  // 获取关注数据
  getData(pageSize = 10) { 
    return new Promise(async (resolve, reject) => {
      const _ = await goodsAPI.queryGoodsCollectionList({
        pageNum: this.data.pageNum,
        pageSize,
      })
      const {records} = await returnStatus(_)
      if (records) {
        this.setData({
          records: [...this.data.records, ...records],
          pageNum: ++this.data.pageNum
        })
        resolve(records.length)
      } else {
        reject(false)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({title}) {
    appInst.setTitle(title)
    this.setData({
      requestObj: {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageNum: 1,
            recommendData: []
          }) : ''
          return this.getData()
        },
      }
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})